<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-form ref="form" :model="form" label-width="80px">
        <el-form-item label="分类名字" prop="categoryName" :rules="{ required: true, message: '分类名字不能为空', trigger: 'blur'}">
          <el-input v-model="form.categoryName" placeholder="请输入用户名称"/>
        </el-form-item>
        <el-form-item label="上级分类" prop="parentId" :rules="{ required: true, message: '上级分类不能为空', trigger: 'blur'}">
          <el-select v-model="form.parentId" placeholder="请选择分类名字">
            <el-option
              v-for="(item,index) in categoryItem"
              :key="index"
              :value="item.categoryId"
              :label="item.categoryName"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="图标" prop="categoryIcon" :rules="{ required: true, message: '图标不能为空', trigger: 'blur'}">
          <el-upload
            class="avatar-uploader"
            :action="uploadUrl"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="form.categoryIcon" :src="form.categoryIcon" class="avatar" :alt="form.categoryIcon">
            <i v-else class="el-icon-plus avatar-uploader-icon"/>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序权重">
          <el-input-number v-model="form.categoryPosition" :min="0" label="排序权重"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="onSubmit">立即保存</el-button>
          <el-button size="small" @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { createCategory, getCategoryDetail, getCategoryPage, updateCategory } from '@/api/Category'

const defaultSettings = require('@/settings.js')
export default {
  data() {
    return {
      uploadUrl: defaultSettings.uploadUrl,
      categoryItem: [],
      form: {
        categoryId: '',
        categoryName: '',
        categoryPosition: '',
        categoryIcon: '',
        parentId: ''
      }
    }
  },
  created() {
    getCategoryPage({ limit: 100, page: 1, type: 0 }).then(res => {
      console.log(res)
      this.categoryItem = res.data.records
    })
    if (this.$route.query.categoryId) {
      this.form.categoryId = this.$route.query.categoryId
      this.getCategory()
    }
  },
  methods: {
    cancel() {
      this.$router.push('/category/list')
    },
    getCategory() {
      getCategoryDetail({ id: this.form.categoryId }).then(res => {
        if (res.code === 200) {
          this.form.categoryId = res.data.categoryId
          this.form.categoryName = res.data.categoryName
          this.form.categoryPosition = res.data.categoryPosition
        } else {
          this.$message.error('网络错误')
        }
      })
    },
    onSubmit() {
      const form = {
        categoryId: this.form.categoryId,
        categoryName: this.form.categoryName,
        categoryPosition: this.form.categoryPosition,
        parentId: this.form.parentId,
        categoryIcon: this.form.categoryIcon,
        categoryLevel: 1,
        categoryStatus: 0
      }
      if (form.categoryId) {
        updateCategory(form).then(res => {
          if (res.code === 200) {
            this.$message.success('保存成功')
            this.$router.push('/category/second')
          } else {
            this.$message.error('网络错误')
          }
        })
      } else {
        createCategory(form).then(res => {
          if (res.code === 200) {
            this.$message.success('保存成功')
            this.$router.push('/category/second')
          } else {
            this.$message.error('网络错误')
          }
        })
      }
    },
    handleAvatarSuccess(res, file) {
      this.form.categoryIcon = res.data
    },
    beforeAvatarUpload(file) {
      const typeItem = ['image/jpeg', 'image/png']
      const isJPG = typeItem.indexOf(file.type)
      const isLt2M = file.size / 1024 / 1024 < 5
      if (isJPG < 0) {
        this.$message.error('上传图片只能是 JPG/PNG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 5MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
